<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>我的任务</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div>
        <div id="pageInTab" class="pageInTab">
            <div tabid="tabA" title="待办事宜" lselected="true">
                <div class="searchArea">
                    <form id="searchBar">
                        <div class="fieldset1 clearFix">
                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">流程名称</div>
                                <div class="field">
                                    <input name="flowDefNm" id="flowDefNm" type="text" class="ui-textbox" value="">
                                </div>
                            </div>
                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">处理节点</div>
                                <div class="field">
                                    <input name="taskNm" id="taskNm" type="text" class="ui-textbox" value="">
                                </div>
                            </div>

                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">承租人</div>
                                <div class="field">
                                    <input name="cstNm" id="cstNm" type="text" class="ui-textbox" value="">
                                </div>
                            </div>

                            <!--
                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">项目名称</div>
                                <div class="field">
                                    <input name="prjNm" id="prjNm" type="text" class="ui-textbox" value="">
                                </div>
                            </div> -->

                            <div class="fieldBox col_50">
                                <div class="label" style="width: 60px;">组织机构</div>
                                <div class="field">
                                    <input name=searchOrgCd id="searchOrgCd" type="text" value="" placeholder="请选择"
                                           ltype="combobox">
                                </div>
                            </div>

                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">业务模式</div>
                                <div class="field">
                                    <select name="businessType" id="businessType">
                                        <option value="" seleted>请选择</option>
                                        <c:forEach items="${businessTypeList }" var="item">
                                            <option value="${item.key }">${item.value }</option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </div>

                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">任务源</div>
                                <div class="field">
                                    <select name="bizResCd" id="bizResCd">
                                        <option value="" seleted>请选择</option>
                                        <c:forEach items="${businessSourceList }" var="item">
                                            <option value="${item.key }">${item.value }</option>
                                        </c:forEach>
                                    </select>
                                </div>
                            </div>

                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">发起人</div>
                                <div class="field">
                                    <input name="initiator" id="initiator" type="text" class="ui-textbox" value="">
                                </div>
                            </div>
                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">发起人工号</div>
                                <div class="field">
                                    <input name="initiatorNo" id="initiatorNo" type="text" class="ui-textbox" value="">
                                </div>
                            </div>
                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">新处理人</div>
                                <div class="field">
                                    <input name="assigneeNm" id="assigneeId" type="text" class="ui-textbox" value="">
                                </div>
                            </div>
                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">新处理人工号</div>
                                <div class="field">
                                    <input name="assigneeNo" id="assigneeNo" type="text" class="ui-textbox" value="">
                                </div>
                            </div>
                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">原处理人</div>
                                <div class="field">
                                    <input name="ownerNm" id="ownerId" type="text" class="ui-textbox" value="">
                                </div>
                            </div>
                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">原处理人工号</div>
                                <div class="field">
                                    <input name="ownerNo" id="ownerNo" type="text" class="ui-textbox" value="">
                                </div>
                            </div>
                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">流程编号</div>
                                <div class="field">
                                    <input name="procNo" id="procNo" type="text" class="ui-textbox" value="">
                                </div>
                            </div>

                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">关键字</div>
                                <div class="field">
                                    <input name="viKey" id="viKey" type="text" class="ui-textbox" value="">
                                </div>
                            </div>

                            <div class="fieldBox">
                                <div class="label" style="width: 60px;">任务分类</div>
                                <div class="field">
                                    <input type="checkbox" id="suspendedId" value="suspended" name="suspendedNm"/>挂起
                                </div>
                                <div class="field" style="margin-left: 10px;">
                                    <input type="checkbox" id="cancelId" value="cancel" name="cancelNm"/>作废
                                </div>
                            </div>

                            <div class="clearFix"></div>
                            <div class="btnsBox">
                                <span id="searchBtn" class="sq-button sq-button-round">查询</span>
                                <span onclick="clearBtn();" class="sq-button sq-button-round">清空</span>
                                <span onclick="changeAssigneeRow()" class="sq-button sq-button-round">变更发起人</span>
                                <span onclick="setAssigneeRow()" class="sq-button sq-button-round">设置处理人</span>
                                <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
                            </div>
                        </div>
                    </form>
                </div>
                <div id="ligergridDB" class="sq-grid"></div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    var gridDB = null;
    var pageInApp = {}; // to 下一级子窗口 接口
    var pageInTab = null;
    var loadedTabs = []
    $(function () {
        $("#exeFlowId").hide();
        $("#privateTaskId").hide();
        initTab();
        initGridDB();//初始化待办列表
        initOrgTree();//注意要放在ligerForm之前加载
        $("#searchBar").ligerForm();
        $("#searchBtn").click(function (event) {
            reloadGrid();
        });
    });

    //重新加载GRID
    function reloadGrid() {
        var form = new liger.get("searchBar");
        var data = form.getData();
        //组织架构显示名称
        /* if(searchOrgTree.treeManager.getSelected()){
         var orgDispNm = searchOrgTree.treeManager.getSelected().data['orgDispNm'];
         if(orgDispNm){
         data["orgDispNm"] = orgDispNm;
         }
         } */
        //业务类型要名字 businessType
        var _businessType = $("#businessType").val();
        //alert(_businessType);
        var _businessTypeName = $("select[name=businessType] option[value='" + _businessType + "']").text();
        if (_businessTypeName == "请选择") {
            _businessTypeName = "";
        }
        data["businessType"] = _businessTypeName;//用中文作为查询条件

        for (name in data) {
            gridDB.setParm(name, data[name]);
        }
        ;
        gridDB.reload();
    }

    //初始化查询条件中的下拉树
    var searchOrgTree;
    function initOrgTree() {
        searchOrgTree = $("#searchOrgCd").ligerComboBox({
            selectBoxWidth: 240,
            selectBoxHeight: 150,
            treeLeafOnly: false, //限制只能选叶节点，父节点不能点击
            tree: {
                ajaxType: 'get',
                idFieldName: 'text',
                checkbox: false,
                parentIcon: "none",
                childIcon: "none"
            }
        });
        var _url = '<%=path%>/system/sysOrg/searchOrgTree.do';
        AjaxUtils.ajaxGet(_url, {}, function (msg) {
            if (msg.success == 1) {
                searchOrgTree.treeManager.setData(msg.data);
                searchOrgTree.clear();
            } else {
                UIUtils.alertError(msg.info);
            }
        }, false);
    }

    //设置代办人,根据当前人指定给谁处理在指定时间内的事情
    function setAgent() {
        $.ligerDialog.SQopen({
            width: 850,
            height: 400,
            title: '设置代办人',
            url: '<%=path%>/flow/flow/toSetAgentUsrPage.do'
        });
    }

    //初始化待办列表
    function initGridDB() {
        $("#ligergridDB").ligerGrid({
            url: '<%=path%>/flow/flow/initTaskDBManageList.do',
            method: 'get',
            columns: [
                {display: '任务源', name: 'taskTypeNm', width: 100, enabledSort: false},
                {display: '流程名称', name: 'processDefinitionNm', minWidth: 200, enabledSort: false},
                {
                    display: '流程编号', name: 'procNo', width: 130, enabledSort: false,
                    render: function (rowdata, rowindex, value, column) {
                        if (StringUtils.isEmpty(value)) {
                            value = "";
                        }
                        var h = '';
                        h = '<a href="javascript:exeRow(';
                        h += "'" + rowindex + "'";
                        h += ')">' + value + '</a>'
                        return h;
                    }
                },
                {display: '处理节点', name: 'name', width: 100, enabledSort: false},
                {display: '发起人', name: 'initiatorNm', width: 60, align: 'left', enabledSort: false},
                {display: '发起时间', name: 'startTime', minWidth: 140, render: renderDate, enabledSort: false},
                {display: '处理状态', name: 'statusNm', minWidth: 100, enabledSort: false},
                {
                    display: '新处理人', name: 'assigneeNm', minWidth: 120, enabledSort: false,
                    render: function (rowdata, rowindex, value) {
                        return UIUtils.showColumnTip(value);
                    }
                },
                {display: '原处理人', name: 'ownerNm', minWidth: 80, enabledSort: false},
                {display: '当前耗时', name: 'durationStr', minWidth: 140, enabledSort: false},
                getOperColumn()
            ],
            pageSize: 10,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: '100%',
            heightDiff: -30,
            //isScroll: false,
            checkbox: true,
            rownumbers: true
        });
        gridDB = liger.get("ligergridDB");
        gridDB.setHeight(100);
        // gridDB.options.isSingleCheck = true;
    }

    //获取最后操作列
    function getOperColumn() {
        return {
            display: '操作',
            width: 200,
            align: 'left',
            render: function (rowdata, rowindex, value) {
                //如果assignee为空则显示签收按钮
                var other = "";
                // other += '<a id="grid_row_private_btn_id" class="sq-button-hasicon" href="javascript:;" onclick="setAssigneeRow(';
                // other += "'" + rowindex + "'";
                // other += ")\"><i class=\"sqBtnIco_45\"></i>" + LogUtils.LOG_FLOW_SET_ASSIGNEE_ROW + "</a>";
                // var change = "";
                // change += '<a id="grid_row_private_btn_id" class="sq-button-hasicon" href="javascript:;" onclick="changeAssigneeRow(';
                // change += "'" + rowindex + "'";
                // change += ")\"><i class=\"sqBtnIco_45\"></i>" + LogUtils.LOG_FLOW_CHANGE_ASSIGNEE_ROW + "</a>";
                var cancel = "";
                cancel += '<a id="grid_row_private_btn_id" class="sq-button-hasicon" href="javascript:;" onclick="setCancel(';
                cancel += "'" + rowindex + "'";
                cancel += ")\"><i class=\"sqBtnIco_5\"></i>" + LogUtils.LOG_CMM_GRID_BTN_ZF + "</a>";
                var recover = "";
                recover += '<a id="grid_row_private_btn_id" class="sq-button-hasicon" href="javascript:;" onclick="setRecover(';
                recover += "'" + rowindex + "'";
                recover += ")\"><i class=\"sqBtnIco_48\"></i>" + LogUtils.LOG_CMM_GRID_BTN_BACK + "</a>";
                /* if($.trim(rowdata.assignee) == ""){
                 other+='<a id="grid_row_private_btn_id" class="sq-button-hasicon" href="javascript:;" onclick="privateRow(';
                 other+="'"+rowindex+"'";
                 other+=")\"><i class=\"sqBtnIco_43\"></i>"+LogUtils.LOG_FLOW_PRIVATE_ROW+"</a>";
                 }else{
                 other+='<a id="grid_row_exe_btn_id" class="sq-button-hasicon" href="javascript:;" onclick="exeRow(';
                 other+="'"+rowindex+"'";
                 other+=")\"><i class=\"sqBtnIco_46\"></i>"+LogUtils.LOG_FLOW_EXE_ROW+"</a>";
                 } */
                if (rowdata.procStatus != "cancel") {
                    other = other +  cancel;
                } else {
                    other = other +  recover;
                }
                return other;
            }
        };
    }

    //处理流程
    function exeRow(_index) {
        var rowdata = gridDB.rows[_index];
        var wait_dialog = UIUtils.alertWaiting(LogUtils.LOG_CMM_DATA_LOADING, function () {
        });
        //非业务参数
        var _name = rowdata.name;
        var _id = rowdata.id;
        //业务参数
        var _processDefinitionId = rowdata.processDefinitionId;//流程定义ID
        var _processInstanceId = rowdata.processInstanceId;//流程实例ID
        var _processInstanceCode = rowdata.procNo;//流程实例编号
        var _taskId = rowdata.taskId;//任务ID
        var _assignable = rowdata.assignable;//是否可分配按钮的标识 true fasle
        var _formUrl = rowdata.formUrl;//表单地址
        var _flowList = rowdata.sequenceFlowVOList;//任务分出连线
        var _processDefinitionKey = rowdata.processDefinitionKey;//流程定义Key
        var _taskDefKey = rowdata.taskDefKey;//任务定义Key
        var _businessKey = rowdata.businessKey;//业务数据ID
        var _taskType = rowdata.taskType;//任务类型标识,后面区分按钮的显示与否
        var _status = rowdata.status;//任务状态
        var _assignee = rowdata.assignee;//任务受理人
        var _businessType = rowdata.businessType;//业务类型
        var _originTaskId = rowdata.originTaskId;//原始任务ID
        var _beanName = rowdata.beanName;//业务Bean
        var _taskDefKeyNm = rowdata.name;
        var _groupShare = rowdata.groupShare;//签收后组内是否可见
        var params = "processDefinitionId=" + _processDefinitionId +
            "&processInstanceId=" + _processInstanceId +
            "&taskId=" + _taskId +
            "&businessKey=" + _businessKey +
            "&assignable=" + _assignable +
            "&formUrl=" + _formUrl +
            "&taskType=" + _taskType +
            "&processDefinitionKey=" + _processDefinitionKey +
            "&taskDefKey=" + _taskDefKey +
            "&taskDefKeyNm=" + _taskDefKeyNm +
            "&status=" + _status +
            "&assignee=" + _assignee +
            "&businessType=" + _businessType +
            "&originTaskId=" + _originTaskId +
            "&beanName=" + _beanName +
            "&flowList=" + JSON.stringify(_flowList) +
            "&grpShr=" + _groupShare +
            "&procNo=" + _processInstanceCode;

        //处理流程关闭后回调
        var callback = function () {
            reloadGrid();
        };
        Add_navTab('tab_flow_exe_' + _taskId, _name, '<%=path%>/flow/flow/toManageExeFlowPage.do?' + params, callback);
        wait_dialog.close();
    }

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
        }
        return dateStr;
    }

    //处理流程
    function setAssigneeRow() {
        var _taskIds=[];
        var searchSysCd = $("#searchSysCd").val();
        var selectedRows = gridDB.getSelectedRows();
        for (var i = 0; i < selectedRows.length; i++) {
            _taskIds.push(selectedRows[i].taskId)
        }
        var This_dialog = $.ligerDialog.SQopen({
            url: "<%=path%>/flow/flow/toCheckboxUsrWinPage.do?isMore=0",
            width: 1000,
            title: "选择组织用户",
            onClosed: function () {
                if (!This_dialog._DATA.usrIds || This_dialog._DATA.usrIds.lenght == 0) {
                    //没有选择任何人说明是关闭了窗口
                } else {
                    setAssigneeRowFunc(_taskIds,  This_dialog.get("_usrs"));
                }
            }
        });
    }

    //重新设置处理人
    function setAssigneeRowFunc(_taskId, _usrs) {
        debugger
        var parms = {
            taskIds: JSON.stringify(_taskId),
            usr: JSON.stringify(_usrs[0])
        }
        var _url = '<%=path%>/flow/flow/modifyAssignTask.do';
        AjaxUtils.ajaxPost(_url, parms, function (msg) {
            if (msg.success == 1) {
                UIUtils.alertSuccess(msg.info);
                reloadGrid();
            } else {
                UIUtils.alertError(msg.info);
            }
        }, true);
    }

    //变更发起人
    function changeAssigneeRow() {
        var searchSysCd = $("#searchSysCd").val();
        var _processInstanceIds=[];
        var selectedRows = gridDB.getSelectedRows();
        for (var i = 0; i < selectedRows.length; i++) {
            _processInstanceIds.push(selectedRows[i].processInstanceId);//流程实例ID
        }
        var This_dialog = $.ligerDialog.SQopen({
            url: "<%=path%>/flow/flow/toCheckboxUsrWinPage.do?isMore=0&sysCd="+searchSysCd,
            width: 1000,
            title: "选择组织用户",
            onClosed: function () {
                if (!This_dialog._DATA.usrIds || This_dialog._DATA.usrIds.lenght == 0) {
                    //没有选择任何人说明是关闭了窗口
                } else {
                    changeAssigneeRowFunc(_processInstanceIds, This_dialog.get("_usrs"));
                }
            }
        });
    }

    function changeAssigneeRowFunc(_processInstanceIds, _usrs) {
        var parms = {
            processInstanceIds: JSON.stringify(_processInstanceIds),
            usr: JSON.stringify(_usrs[0])
        }
        var _url = '<%=path%>/flow/flow/modifyInitiator.do';
        AjaxUtils.ajaxPost(_url, parms, function (msg) {
            if (msg.success == 1) {
                UIUtils.alertSuccess(msg.info);
                reloadGrid();
            } else {
                UIUtils.alertError(msg.info);
            }
        }, true);
    }

    function setCancel(_index) {
        if (!_index)return;
        $.ligerDialog.confirm('确定作废吗?', function (flag) {
            if (flag == false)return;
            var wait_dialog;
            var rowdata = gridDB.rows[_index];
            //业务参数
            var _taskId = rowdata.taskId;//任务ID
            var _processInstanceId = rowdata.processInstanceId;//流程实例ID

            var _url = "<%=path%>/flow/flow/modifyFlowCancel.do";
            AjaxUtils.ajaxPost(_url, {
                taskId: _taskId,
                processInstanceId: _processInstanceId
            }, function (msg) {
                if (msg.success == 1) {
                    reloadGrid();
                } else {
                    UIUtils.alertError(msg.info);
                }
                wait_dialog.close();
            }, true);
        });
    }

    function setRecover(_index) {
        if (!_index)return;
        $.ligerDialog.confirm('确定恢复吗?', function (flag) {
            if (flag == false)return;
            var wait_dialog;
            var rowdata = gridDB.rows[_index];
            //业务参数
            var _taskId = rowdata.taskId;//任务ID
            var _processInstanceId = rowdata.processInstanceId;//流程实例ID

            var _url = "<%=path%>/flow/flow/modifyFlowRecover.do";
            AjaxUtils.ajaxPost(_url, {
                taskId: _taskId,
                processInstanceId: _processInstanceId
            }, function (msg) {
                if (msg.success == 1) {
                    reloadGrid();
                } else {
                    UIUtils.alertError(msg.info);
                }
                wait_dialog.close();
            }, true);
        });
    }

    function clearBtn() {
        $("#resetBtn").click();  //重置 input-text
        var form = liger.get("searchBar");
        $("#orgCd").val("");
        form.setData({  //重置select等特殊表单
            //"orgCd": " ",
            "businessType": " ",
            "bizResCd": " "
        });
        searchOrgTree.clear();
    }

    /* 	function iframeResize () {
     if (parent.mainApp) {
     parent.mainApp.parentTab.onResize();
     };
     } */


    //初始化Tab页
    function initTab() {
        pageInTab = $("#pageInTab").ligerTab({
            height: 'auto',
            contextmenu: false,
            minIframeHeight: GET_minIframeHeight(),
            onAfterSelectTabItem: function (tabid) {
                SetIframe_height();
            }
        });

        $.extend(true, pageInApp, {
            pageInTab: pageInTab
        });
    }
</script>

</body>
</html>